//
//  MainTabBar.m
//  weibo-OC
//
//  Created by diyuyma on 16/2/20.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "MainTabBar.h"

@interface MainTabBar ()

@end

@implementation MainTabBar

// 懒加载
- (UIButton *)composeBtn {
    
    if (_composeBtn == nil) {
        
        UIButton *btn = [[UIButton alloc] init];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [btn sizeToFit];
        _composeBtn = btn;
    }
    
    return _composeBtn;
}


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self setupUI];
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    return self;
}

- (void)setupUI {
    
    [self addSubview:self.composeBtn];
}

#pragma mark 加号按钮点击事件
//- (void)plusCilck {
//    // 通知代理
//    if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPluseButton:)]) {
//        [self.tabBarDelegate tabBarDidClickPluseButton:self];
//    }
//}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    // 手动修改所有按钮位置
    // 遍历所有视图
    CGFloat w = self.bounds.size.width / 5;
    CGFloat h = self.bounds.size.height;
    CGRect rect = CGRectMake(0, 0, w, h);
    
    // 定义递增变量
    CGFloat index = 0;
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            subView.frame = CGRectOffset(rect, index * w, 0);
            index += index == 1 ? 2 : 1;
        }
        
        // 设置撰写按钮的位置
        self.composeBtn.frame = CGRectOffset(rect, w * 2, 0);
        [self bringSubviewToFront:self.composeBtn];
    }

}

@end
